/* eslint-disable 
import utils from '../../assets/scripts/utils'
// Vue.prototype.$utils = utils // main.js中全局引入
let id = utils.getUrlKey('id')
*/
export default {
    getUrlKey: function (name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
    },
    //exdays  原单位是毫秒
    setCookie: function (cname, cvalue, exdays) {
        var d = new Date();
        //console.log(d);
        //d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        //几分钟
        d.setTime(d.getTime() + (exdays * 1000 * 60));
        //var expires = "expires=" + d.toUTCString();
        //var expires = "expires=" + d.toUTCString();
        //console.log("localtime" + d.toLocaleString());
        var expires = "expires=" + d;
        //console.info(cname + "=" + cvalue + "; " + expires);
        //document.cookie="example=1; max-age=0; domain=.test.com;path=/"
        document.cookie = cname + "=" + cvalue + ";domain=.sso.com" + "; " + expires;
        //console.info(document.cookie);
    },
    //获取cookie
    getCookie: function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        console.log("获取cookie,现在循环")
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            //console.log(c)
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    },
    //清除cookie
    clearCookie: function () {
        this.setCookie("username", "", -1);
    },
    //把url后面的参数去掉
    delUrlParams: function (url) {
        //如果没有？ 会返回-1
        //console.log("?????===="+url.indexOf('?'))
        if (url.indexOf('?') != -1) {
            return url.substring(0, url.indexOf('?'));
        } else {
            return url
        }
    },
    //得到url域名
    getUrl: function (url) {
        var domain = url.split('/'); //以“/”进行分割
        if (domain[2]) {
            domain = domain[0] + '//' + domain[2] + '/';
        } else {
            domain = ''; //如果url不正确就取空
        }
        return domain;
    },
    //得到随机整数
    GetRandomNum(Min, Max) {
        var Range = Max - Min;
        var Rand = Math.random();
        //round +0.5  再取整
        return (Min + Math.round(Rand * Range));
    },
    //随机字符串
    randomString(len) {
        len = len || 46;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprswxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length;
        var pwd = '';
        for (let i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    },
    fillCookReturnData() {

        var token = localStorage.getItem('token');


        //var cookies = {
         //   tmcb: this.getCookie('tmcb'),
         //   nickname: this.getCookie('nickname')
       //}
       var cookies=null;
       //console.log("sessionStorage---"+sessionStorage.getItem('mycookies'));
        if(sessionStorage.getItem('mycookies')!='undefined'){
            console.log("cookie不为空"+cookies);
            cookies=JSON.parse(sessionStorage.getItem('mycookies')) ;
        }
      
       //console.log(cookies);
        
        
        var cookieArry = document.cookie.split(";");
        var setCookNum;
        if (cookieArry.length < 30) {
            setCookNum = 30 - cookieArry.length;
        }

        console.log("cookie数---" + setCookNum)
        for (let i = 1; i <= setCookNum; i++) {
            let k = this.randomString(this.GetRandomNum(1, 6));
            cookies[k] = this.randomString(this.GetRandomNum(5, 46));
            //15天 21600分钟
            this.setCookie(k, cookies[k], 21600)
        }
        if(cookies){
            
            sessionStorage.setItem('mycookies',JSON.stringify(cookies))
        }
        
        
        
        //this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("storedata"))))
        var data = {
            token: token,
            cookies: cookies
        }

        return data;

    }
}